package com.grt192.mechanism.hauntedhouse;

import com.grt192.actuator.GRTLED;
import com.grt192.controller.hauntedhouse.HHLEDController;
import com.grt192.core.Command;
import com.grt192.core.Mechanism;
import com.grt192.sensor.GRTSwitch;
import com.grt192.core.Sensor;

/**
 * Represents the Operator Interface for main master/auto button light control
 *
 * @autor data, ajc, nik.lele
 */
public class HHLEDMechanism extends Mechanism {

    public static final String MASTER_BUTTON_ID = "MasterButton";
    public static final String AUTO_BUTTON_ID = "AutoButton";
    public static final String MASTER_LED_ID = "MasterLED";
    public static final String AUTO_LED_ID = "AutoLED";

    private static final int MASTER_BUTTON_PIN = 3;
    private static final int AUTO_BUTTON_PIN = 9;
    private static final int MASTER_LED_PIN = 1;
    private static final int AUTO_LED_PIN = 2;

    public static final double SWITCH_HIT = Sensor.FALSE;//false or true?
    public static final double BUTTON_HIT = Sensor.FALSE;//false or true?
    private GRTLED masterLED;
    private GRTLED autoLED;

    private GRTSwitch masterButton;
    private GRTSwitch autoButton;

    public HHLEDMechanism() {

        //initialize actuators and sensors
        masterButton = new GRTSwitch(MASTER_BUTTON_PIN, 50);
        autoButton = new GRTSwitch(AUTO_BUTTON_PIN, 50);
        masterLED = new GRTLED(MASTER_LED_PIN);
        autoLED = new GRTLED(AUTO_LED_PIN);

        //add actuators and sensors
        addSensor(MASTER_BUTTON_ID, masterButton);
        addSensor(AUTO_BUTTON_ID, autoButton);
        addActuator(MASTER_LED_ID, masterLED);
        addActuator(AUTO_LED_ID, autoLED);

        //start actuators and sensors
        masterButton.start();
        autoButton.start();
        masterLED.start();
        autoLED.start();

        System.out.println("Turning on LEDs");
        //read default states from the Controller
        masterLED.enqueueCommand(new Command
                (HHLEDController.MASTER_DEFAULT_STATE? GRTLED.TURN_ON: GRTLED.TURN_OFF));
        autoLED.enqueueCommand(new Command
                (HHLEDController.AUTO_DEFAULT_STATE? GRTLED.TURN_ON: GRTLED.TURN_OFF));
    }

//    public boolean hitSwitch(String ID) {
//        if (!ID.equals(MASTER_BUTTON_ID) && !ID.equals(AUTO_BUTTON_ID)) {
//            return getSensor(ID).getState("State") == SWITCH_HIT;
//        } else {
//            return false;
//        }
//    }
//    public boolean hitButton(String ID) {
//        if (ID.equals(MASTER_BUTTON_ID) || ID.equals(AUTO_BUTTON_ID)) {
//            return getSensor(ID).getState("State") == BUTTON_HIT;
//        } else {
//            return false;
//        }
//    }

    public void setMasterLED(boolean on) {
        if (on) {
            masterLED.enqueueCommand(new Command(GRTLED.TURN_ON));
        } else {
            masterLED.enqueueCommand(new Command(GRTLED.TURN_OFF));
        }
    }
    public void setAutoLED(boolean on) {
        if (on) {
            autoLED.enqueueCommand(new Command(GRTLED.TURN_ON));
        } else {
            autoLED.enqueueCommand(new Command(GRTLED.TURN_OFF));
        }
    }
}
